package com.example.paopao.consumer.memer.dao.persist.repository.impl;

import com.example.paopao.consumer.memer.dao.persist.mapper.LookWorkMapper;
import com.example.paopao.consumer.memer.dao.persist.repository.ILookWorkRepository;
import com.example.paopao.consumer.memer.pojo.dto.ReleaseWorkDTO;
import com.example.paopao.consumer.memer.pojo.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public class LookWorkRepositoryImpl implements ILookWorkRepository {

    @Autowired
    private LookWorkMapper mapper;

    @Override
    public int setSeleaseWork(ReleaseWorkDTO releaseWorkDTO) {
        int countRelease=mapper.getCountReleaseById()+1;
        int countManage=mapper.getCountManageIById()+1;
        int countImage=mapper.getCountImageRById()+1;
        int countAddress=mapper.getCountAddressByid()+1;

        ReleaseHiring releaseHiring=new ReleaseHiring();
        releaseHiring.setId(countRelease);
        releaseHiring.setUserId(releaseWorkDTO.getUserId());
        releaseHiring.setAddressId(countAddress);
        releaseHiring.setViewCount(0);
        releaseHiring.setContact(releaseWorkDTO.getPhone());
        int salary = releaseWorkDTO.getSalary().intValue();
        releaseHiring.setSalary(salary);
        releaseHiring.setSettle(releaseWorkDTO.getSettlement());
        releaseHiring.setContent(releaseWorkDTO.getContent());
        releaseHiring.setNumber(releaseWorkDTO.getNumber());
        releaseHiring.setTerm(releaseWorkDTO.getTerm());
        releaseHiring.setIsDisplay(true);
        releaseHiring.setCollect(false);
        releaseHiring.setViewCount(0);
        releaseHiring.setRelseaseTime(new Date());
        releaseHiring.setSort(1);
        releaseHiring.setTitle(releaseWorkDTO.getContent());
        mapper.setAddNewSeleaseWork(releaseHiring);


//        userId=1, selectedwork=[包装工, 操作工],
//        district=[2, 52, 500], phone=131231231,
//        salary=11213, settlement=元/月,
//        content=<p>三大阿斯顿阿达大大</p>, number=3-5人, term=5-14天,
        AddressI addressI=new AddressI();
        addressI.setId(countAddress);
        addressI.setEconomizeId(releaseWorkDTO.getDistrict().get(0));
        addressI.setCity(releaseWorkDTO.getDistrict().get(1));
        addressI.setClassify(releaseWorkDTO.getDistrict().get(2));
        addressI.setReleaseId(countRelease);
        mapper.setAddNewAddressI(addressI);

        Integer index;
        index=releaseWorkDTO.getIndustryId().get(0);
        boolean isValue;
        for (int i = 0; i < releaseWorkDTO.getIndustryId().size(); i++) {
            isValue=true;
            if (index==releaseWorkDTO.getIndustryId().get(i)){
                isValue=false;
            }
            if (isValue || i==0){
                ManageMintr manageMintr=new ManageMintr();
                manageMintr.setRinfoId(countRelease);
                manageMintr.setInstrId(index);
                mapper.setAddNewIndustryI(manageMintr);
                index=releaseWorkDTO.getIndustryId().get(i);
            }
        }

        int industryIndex=0;
        for (Object s : releaseWorkDTO.getSelectedwork()) {
            ManageI manageI=new ManageI();
            manageI.setId(countManage++);
            String substring = s.toString().substring(1, s.toString().length() - 1);
            String[] split = substring.split(",");

//            id=3024, detailName=其他生产质量人员, peId=2003, list=[]
            for (int i = 0; i < split.length; i++) {
                String[] split1 = split[i].split("=");
                for (int j = 1; j < split1.length; j+=2) {
                    switch (i){
                        case 0:
                            int value = Integer.parseInt(split1[1]);
                            manageI.setJobId(value);
                            break;
                        case 1:
                            manageI.setWorkName(split1[j]);
                            break;
                    }
                }
            }
            manageI.setRinfoId(countRelease);
            manageI.setRintrId(releaseWorkDTO.getIndustryId().get(industryIndex++));
            mapper.setAddNewManageI(manageI);
        }

        List<String> imageUrl = releaseWorkDTO.getImageUrl();
        if(imageUrl!=null && imageUrl.size()!=0){
            ImageR imageR=new ImageR();
            imageR.setId(countImage);
            imageR.setWorkImg(imageUrl.get(0));
            imageR.setPrijectImg(imageUrl.get(1));
            imageR.setContractImg(imageUrl.get(2));
            imageR.setSeleaseId(countRelease);
            mapper.setAddNewImageI(imageR);
        }
        return 1;
    }
}
